package cn.reineko.csmall.passport.controller;

import cn.reineko.csmall.passport.pojo.dto.AdminAddNewDTO;
import cn.reineko.csmall.passport.pojo.dto.AdminUpdateDTO;
import cn.reineko.csmall.passport.pojo.dto.AdminUpdatePasswordDTO;
import cn.reineko.csmall.passport.pojo.vo.AdminListItemVO;
import cn.reineko.csmall.passport.service.IAdminService;
import cn.reineko.csmall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理admin的相关请求
 *
 * @author reineko
 * @version 1.0.0
 */
@Slf4j
@Api(tags = "01. 管理员管理模块")
@Validated
@RestController
@RequestMapping("/admins")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Validated AdminAddNewDTO adminAddNewDTO) {
        log.trace("开始处理【添加管理员】的请求，参数：{}", adminAddNewDTO);
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除管理员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        log.trace("开始处理【删除管理员】的请求");
        adminService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("启用管理员")
    @ApiOperationSupport(order = 301)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.trace("开始执行【启用管理员】的请求");
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 302)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.trace("开始执行【禁用管理员】的请求");
        adminService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改密码【超管修改，不做密码对比】")
    @ApiOperationSupport(order = 303)
    @PostMapping("/updatePassword")
    public JsonResult<Void> updatePassword(@Validated AdminUpdatePasswordDTO adminUpdatePasswordDTO) {
        log.trace("开始执行【修改密码】的请求");
        adminService.updatePasswordById(adminUpdatePasswordDTO);
        return JsonResult.ok();
    }

    @ApiOperation("修改管理员的基本信息")
    @ApiOperationSupport(order = 304)
    @PostMapping("/updateById")
    public JsonResult<Void> updateById(@Validated AdminUpdateDTO adminUpdateDTO) {
        log.trace("开始处理【修改管理员基本信息】的请求");
        adminService.updateById(adminUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<AdminListItemVO>> list() {
        log.trace("开始处理【查询所有管理员信息】的请求");
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }

}
